/**
 * MainPanel.java
 */

package edu.gatech.coc.betameisters.main;

import java.awt.CardLayout;
import java.awt.Dimension;

import javax.swing.JPanel;

import edu.gatech.coc.betameisters.infoholders.Appointment;
import edu.gatech.coc.betameisters.infoholders.Constants;
import edu.gatech.coc.betameisters.infoholders.DoctorsOrders;
import edu.gatech.coc.betameisters.infoholders.Invoice;
import edu.gatech.coc.betameisters.infoholders.MedicalHistory;
import edu.gatech.coc.betameisters.infoholders.Prescription;
import edu.gatech.coc.betameisters.infoholders.Treatment;
import edu.gatech.coc.betameisters.people.Doctor;
import edu.gatech.coc.betameisters.people.Nurse;
import edu.gatech.coc.betameisters.people.Patient;
import edu.gatech.coc.betameisters.people.Person;
import edu.gatech.coc.betameisters.people.SystemAdmin;
import edu.gatech.coc.betameisters.userinterfaces.EditAppointments;
import edu.gatech.coc.betameisters.userinterfaces.EditDoctorsOrders;
import edu.gatech.coc.betameisters.userinterfaces.EditMedicalHist;
import edu.gatech.coc.betameisters.userinterfaces.EditUser;
import edu.gatech.coc.betameisters.userinterfaces.InvoiceView;
import edu.gatech.coc.betameisters.userinterfaces.LoginPanel;
import edu.gatech.coc.betameisters.userinterfaces.NursesUI;
import edu.gatech.coc.betameisters.userinterfaces.PatientsUI;
import edu.gatech.coc.betameisters.userinterfaces.PrescriptionScreenUI;
import edu.gatech.coc.betameisters.userinterfaces.SystemAdminsUI;
import edu.gatech.coc.betameisters.userinterfaces.TreatmentScreenUI;

/**
 * The MainPanel containing all UIs.
 * 
 * @author Suren Nihalani
 * @version 1.0
 */
public class MainPanel extends JPanel {
	private Person loggedIn;
	private Person local;
	private DoctorsOrders item;
	private Treatment treatment;
	private Prescription prescrip;
	/**
	 * Just made eclipse add this because I don't like any warnings.
	 */
	private static final long serialVersionUID = -5424674747102991242L;

	/**
	 * Create the panel.
	 */
	private NursesUI myDoctorsUI;

	/** The my nurses ui. */
	private NursesUI myNursesUI;

	/** The my patients ui. */
	private PatientsUI myPatientsUI;

	/** The my admin ui. */
	private SystemAdminsUI myAdminUI;

	/** The appointments editor. */
	private EditAppointments aEditor;

	/** The doctors order editor. */
	private EditDoctorsOrders dEditor;

	/** The Medical History editor. */
	private EditMedicalHist mEditor;

	/** The Invoice Printer */
	private InvoiceView invoice;
	
	/** The User editor. */
	private EditUser uEditor;
	/** Login screen */
	private LoginPanel LoginScreen;
	/** The Card layout. */
	private final CardLayout myLayout;
	private TreatmentScreenUI tEditor;
	private PrescriptionScreenUI pEditor;

	/**
	 * Constructor method.
	 */
	public MainPanel() {
		setLayout(new CardLayout());
		myDoctorsUI = new NursesUI(new Doctor(), this);
		myNursesUI = new NursesUI(new Nurse(), this);
		myPatientsUI = (new PatientsUI(new Patient(), this));
		myAdminUI = new SystemAdminsUI(new SystemAdmin(), this);
		aEditor = new EditAppointments(new Appointment(), this);
		dEditor = new EditDoctorsOrders(new DoctorsOrders(" ", " ", " "), this);
		mEditor = new EditMedicalHist(new Patient(), new MedicalHistory(), this);
		tEditor = new TreatmentScreenUI(new Treatment(
				Constants.VisitType.OFFICEVISIT), this);
		uEditor = new EditUser(null, this);
		pEditor = new PrescriptionScreenUI(new Doctor(),new Prescription(new Doctor(), new Patient()), this);
		LoginScreen = new LoginPanel();
		invoice = new InvoiceView(new Invoice(""), this);
		add(myDoctorsUI, Constants.DOCTOR_PANEL);
		add(myNursesUI, Constants.NURSE_PANEL);
		add(myPatientsUI, Constants.PATIENT_PANEL);
		add(myAdminUI, Constants.ADMIN_PANEL);
		add(aEditor, Constants.APPOINTMENTS_EDITOR);
		add(dEditor, Constants.DOCTORSORDERS_EDITOR);
		add(mEditor, Constants.MEDICALHISTORY_EDITOR);
		add(uEditor, Constants.USER_EDITOR);
		add(LoginScreen, Constants.LOGIN_SCREEN);
		add(tEditor, Constants.TREATMENTS);
		add(pEditor, Constants.PRESCRIPTION);
		add(invoice, Constants.INVOICE);
		myLayout = (CardLayout) getLayout();
		loggedIn = null;
		setPreferredSize(Constants.WINDOWSIZE);
		setMaximumSize(Constants.WINDOWSIZE);
		setMinimumSize(Constants.WINDOWSIZE);
	}

	/**
	 * Sets the patient.
	 * 
	 * @param theNewPatient
	 *            the new patient
	 */
	public void setPatient(Patient theNewPatient) {
		myPatientsUI = new PatientsUI(theNewPatient, this);
		myLayout.show(this, Constants.PATIENT_PANEL);
	}

	/**
	 * Sets the doctor.
	 * 
	 * @param theNewDoctor
	 *            the new doctor
	 */
	public void setDoctor(Doctor theNewDoctor) {
		inputUser(theNewDoctor);
		myDoctorsUI = new NursesUI(theNewDoctor, this);
		myLayout.show(this, Constants.DOCTOR_PANEL);
	}

	/**
	 * Sets the nurse.
	 * 
	 * @param newNurse
	 *            the new nurse
	 */
	public void setNurse(Nurse newNurse) {
		inputUser(newNurse);
		myNursesUI = new NursesUI(newNurse, this);
		myLayout.show(this, Constants.NURSE_PANEL);
	}

	/**
	 * Sets the my admin.
	 * 
	 * @param newSystemAdmin
	 *            the new my admin
	 */
	public void setMyAdmin(SystemAdmin newSystemAdmin) {
		inputUser(newSystemAdmin);
		myAdminUI = new SystemAdminsUI(newSystemAdmin, this);
		myLayout.show(this, Constants.ADMIN_PANEL);
	}

	/**
	 * Sets the appointment.
	 * 
	 * @param theAppointment
	 *            the new appointment
	 */
	public void setAppointment(Appointment theAppointment) {
		aEditor = new EditAppointments(theAppointment, this);
		myLayout.show(this, Constants.APPOINTMENTS_EDITOR);
	}

	/**
	 * Sets the user.
	 * 
	 * @param p
	 *            the new user
	 */
	public void setUser(Person p) {
		uEditor = new EditUser(p, this);
		myLayout.show(this, Constants.USER_EDITOR);
	}

	/**
	 * Sets the doctors order.
	 * 
	 * @param newOrder
	 *            the new doctors order
	 */
	public void setDoctorsOrder(DoctorsOrders newOrder) {
		dEditor = new EditDoctorsOrders(newOrder, this);
		myLayout.show(this, Constants.DOCTORSORDERS_EDITOR);
	}

	public void setMedicalHistory(Patient p, MedicalHistory mh) {
		mEditor = new EditMedicalHist(p, mh, this);
		myLayout.show(this, Constants.MEDICALHISTORY_EDITOR);
	}

	public void loginScreen() {
		LoginScreen = new LoginPanel();
		myLayout.show(this, Constants.LOGIN_SCREEN);
	}

	public void inputUser(Person aUser) {
		loggedIn = aUser;
	}

	public Person getUser() {
		return loggedIn;
	}
	
	public void setLocal(Person aLocal){
		local = aLocal;
	}
	
	public Person getLocal(){
		return local;
	}

	/**
	 * @param tEditor
	 *            the tEditor to set
	 */
	public void settEditor(Treatment aTreat) {
		tEditor = new TreatmentScreenUI(aTreat, this);
		myLayout.show(this, Constants.TREATMENTS);
	}

	/**
	 * @param pEditor the pEditor to set
	 */
	public void setpEditor(Prescription aPrescrip) {
		pEditor = new PrescriptionScreenUI((Doctor) loggedIn , aPrescrip, this);
		myLayout.show(this, Constants.PRESCRIPTION);
	}

	/**
	 * @param item the item to set
	 */
	public void setItem(DoctorsOrders item) {
		this.item = item;
	}

	/**
	 * @return the item
	 */
	public DoctorsOrders getItem() {
		return item;
	}

	/**
	 * @param treatment the treatment to set
	 */
	public void setTreatment(Treatment treatment) {
		this.treatment = treatment;
		if(loggedIn.getUserType() == Constants.UserType.DOCTOR){
			item.setTreatmentOrder(treatment);
		}
	}

	/**
	 * @return the treatment
	 */
	public Treatment getTreatment() {
		return treatment;
	}

	/**
	 * @param prescrip the prescrip to set
	 */
	public void setPrescrip(Prescription prescrip) {
		this.prescrip = prescrip;
		item.setTreatmentOrder(treatment);
	}

	/**
	 * @return the prescrip
	 */
	public Prescription getPrescrip() {
		return prescrip;
	}
	
	/**
	 * 
	 * @param anInvoice
	 */
	public void showInvoice(Invoice anInvoice){
		invoice = new InvoiceView(anInvoice, this);
		myLayout.show(this, Constants.INVOICE);
	}
}
